package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.dto.CheckGroupDto;
import com.itheima.common.dto.CheckItemDto;
import com.itheima.common.entity.PageResult;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.GroupItemMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.ICheckGroupService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 检查组Service业务层处理
 *
 * @author itheima
 * @date 2025-03-22
 */
@Service
public class CheckGroupServiceImpl implements ICheckGroupService {
    @Autowired
    private CheckGroupMapper checkGroupMapper;

    @Autowired
    private GroupItemMapper groupItemMapper;

    /**
     * 查询检查组
     *
     * @param id 检查组主键
     * @return 检查组
     */
    @Override
    public CheckGroup selectCheckGroupById(Integer id) {
        return checkGroupMapper.selectCheckGroupById(id);
    }

    /**
     * 查询检查组列表
     *
     * @param checkGroupDto 检查组
     * @return 检查组
     */
    @Override
    public PageResult selectCheckGroupList(CheckGroupDto checkGroupDto) {

        //如果checkItemDto为空，则设置默认值
        if (checkGroupDto == null || checkGroupDto.getCurrentPage() == null || checkGroupDto.getPageSize() == null) {
            checkGroupDto = new CheckGroupDto();
            checkGroupDto.setCurrentPage(1);
            checkGroupDto.setPageSize(10);
        }

        PageHelper.startPage(checkGroupDto.getCurrentPage(), checkGroupDto.getPageSize());
        CheckGroup checkGroup = new CheckGroup();
        BeanUtils.copyProperties(checkGroupDto, checkGroup);
        Page<CheckGroup> checkGroups = (Page<CheckGroup>) checkGroupMapper.selectCheckGroupList(checkGroup);
        return new PageResult(checkGroups.getTotal(), checkGroups.getResult());
    }

    /**
     * 新增检查组
     *
     * @param checkitemIds 关联检查项id
     * @param checkGroup   检查组
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCheckGroup(List<Integer> checkitemIds, CheckGroup checkGroup) {

        int rows = checkGroupMapper.insertCheckGroup(checkGroup);

        if (checkitemIds != null && checkitemIds.size() > 0) {
            for (Integer checkitemId : checkitemIds) {
                Integer checkGroupId = checkGroup.getId();
                groupItemMapper.insert(checkitemId, checkGroupId);
            }
        }

        return rows;
    }

    /**
     * 修改检查组
     * @param checkitemIds 检查项id集合
     * @param checkGroup 检查组
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCheckGroup(List<Integer> checkitemIds, CheckGroup checkGroup) {

        Integer checkGroupId = checkGroup.getId();
        groupItemMapper.deleteByGroupId(checkGroupId);
        if (checkitemIds != null && checkitemIds.size() > 0) {
            for (Integer checkitemId : checkitemIds) {
                groupItemMapper.insert(checkitemId, checkGroupId);
            }
        }

        return checkGroupMapper.updateCheckGroup(checkGroup);
    }

    /**
     * 批量删除检查组
     *
     * @param ids 需要删除的检查组主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCheckGroupByIds(Integer[] ids) {
        if (ids.length > 0) {
            for (Integer id : ids) {
                //查询检查组关联的检查项
                List<CheckItem> checkItemList = checkGroupMapper.selectCheckItemByGroupId(id);
                if (!checkItemList.isEmpty()) {
                    throw new RuntimeException("检查组关联检查项，不能删除");
                }
                checkGroupMapper.deleteCheckGroupById(id);
            }
        }
        return 1;
    }

    /**
     * 删除检查组信息
     *
     * @param id 检查组主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCheckGroupById(Integer id) {
        return checkGroupMapper.deleteCheckGroupById(id);
    }

    /**
     * 根据套餐id查询检查组id集合
     */
    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(Integer setmealId) {
        List<Integer> checkGroupIds = checkGroupMapper.findCheckGroupIdsBySetmealId(setmealId);
        return checkGroupIds;
    }

    /**
     * 查询所有检查组
     */
    @Override
    public List<CheckGroup> selectCheckGroupAll() {
        List<CheckGroup> checkGroupList = checkGroupMapper.selectCheckGroupAll();
        return checkGroupList;
    }

}
